package cn.iocoder.yudao.module.erp.convert.deppurchase;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.deppurchase.vo.DepPurchaseRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.deppurchase.DepPurchaseDO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface DeptPurchaseConvert {

    DeptPurchaseConvert INSTANCE = Mappers.getMapper(DeptPurchaseConvert.class);


    default List<DepPurchaseRespVO> convertList(List<DepPurchaseDO> list, Map<Long, AdminUserRespDTO> userMap, Map<Long, DeptRespDTO> deptMap) {

        return CollectionUtils.convertList(list, depPurchaseDO -> convert(depPurchaseDO, userMap.get(depPurchaseDO.getNewId()), deptMap.get(depPurchaseDO.getNewDeptId())));
    }

    default DepPurchaseRespVO convert(DepPurchaseDO depPurchaseDO, AdminUserRespDTO adminUserRespDTO, DeptRespDTO deptRespDTO) {
        DepPurchaseRespVO depPurchaseRespVO = BeanUtils.toBean(depPurchaseDO, DepPurchaseRespVO.class);
        if (adminUserRespDTO != null) {
            depPurchaseRespVO.setCreatorName(adminUserRespDTO.getNickname());
        }
        if (deptRespDTO != null) {
            depPurchaseRespVO.setDeptName(deptRespDTO.getName());
        }

        return depPurchaseRespVO;
    }
}
